package ru.housewife.service.dishes;

import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.EatingMenuDishRecipeForm;
import ru.housewife.model.EatingMenuDishRecipe;

/**
 * TODO: add description
 *
 * @author Alexander Troshanin
 */
public class UpdateEatingMenuDishCount implements HibernateAction<EatingMenuDishRecipe> {

    private EatingMenuDishRecipeForm eatingMenuRecipeForm;

    public UpdateEatingMenuDishCount(EatingMenuDishRecipeForm eatingMenuRecipeForm) {
        this.eatingMenuRecipeForm = eatingMenuRecipeForm;
    }

    @Override
    public EatingMenuDishRecipe execute(Session session) {
        EatingMenuDishRecipe eatingMenuRecipe = (EatingMenuDishRecipe) session.get(EatingMenuDishRecipe.class, eatingMenuRecipeForm.getId());
        if (eatingMenuRecipe == null) {
            return null;
        }
        eatingMenuRecipe.setCount(eatingMenuRecipeForm.getCount());
        session.save(eatingMenuRecipe);
        return eatingMenuRecipe;
    }
}
